<?php
function list_karyawan_realisasi(){
	$periode_id =get_periode_absen();
	$periode = my_get_data_by_id('pr_periode','periode_id',$periode_id);
	$bulan = (int) date('m' , strtotime($periode['tanggal']));
	$tahun = (int) date('Y' , strtotime($periode['tanggal']));
	$end=$periode['tahun'].'-'. sprintf( '%02d',$bulan) .'-10' ;
	$header1 = array(
		'#'=>array('style'=>'border-bottom:2px solid;width:3%'),
		'nik'=>array('style'=>'border-bottom:2px solid;width:8%'),
		'nama'=>array('style'=>'border-bottom:2px solid;width:20%'),
	//	'WK'=>array('style'=>'border-bottom:2px solid;width:3%'),
	//	'KEL'=>array('style'=>'border-bottom:2px solid;width:3%'),
		  
	);
	$header2= array();
	$tanggals = list_kalender($periode['dari'] , $periode['sampai']  );
	$tanggals = array_unique($tanggals) ;
	foreach($tanggals as $tgl){ 
		$header2[date('d',strtotime($tgl) )] = array('style'=>'border-bottom:2px solid;width:1%');
	}
	$header3 = array(' '=>array('style'=>'border-bottom:2px solid;width:3%'));
	$header = $header1 + $header2 + $header3;
	if(isset($_GET['key'])){
		$query = "
			SELECT * FROM karyawan a 
			INNER JOIN biodata b 
			ON a.biodata_id = b.biodata_id
			WHERE a.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)	
			AND a.karyawan_id NOT IN (select karyawan_id FROM pemberhentian  WHERE TMT <= '{$end}') 	
			AND ( b.nama LIKE '%{$_GET['key']}%' OR a.nik='{$_GET['key']}')
			ORDER BY a.nik
		";
	}else{
		$query = "
			SELECT * FROM karyawan a 
			INNER JOIN biodata b 
			ON a.biodata_id = b.biodata_id
			WHERE a.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)	
			AND a.karyawan_id NOT IN (select karyawan_id FROM pemberhentian  WHERE TMT <= '{$end}') 	
			ORDER BY a.nik
		";
	}
	$result = my_query($query);
	$total_records = my_num_rows($result );
	$scroll_page =5;   
	$per_page =24;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	$pager_url  ="index.php?com=jadwal_wk_karyawan&task={$_GET['task']}&key={$_GET['key']}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	$kgPagerOBJ = new kgPager();
	$kgPagerOBJ->pager_set(
		$pager_url, 
		$total_records, 
		$scroll_page, 
		$per_page, 
		$current_page, 
		$inactive_page_tag, 
		$previous_page_text, 
		$next_page_text, 
		$first_page_text, 
		$last_page_text ,$pager_url_last); 
	 		
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	$row = array(); 
	$i = ($current_page  - 1 ) * 24; 
	while($ey = my_fetch_array($result )){
		$i++;
		$karyawan =  loaddata_karyawan($ey['karyawan_id']); 
 
		$detailproperty = array(
			'href'=>'index.php?com='.$_GET['com'].'&task=list_detail&karyawan_id='.$ey['karyawan_id'], 
			'title'=>'Detail'
		);
		$detail_button = button_icon( 'b_props.png' , $detailproperty  ); 
		$row1 = array(
			'#'=>position_text_align ($i, 'center'), 
			'nik'=>position_text_align( $karyawan['nik'], 'center'),
			'nama'=>substr($karyawan['nama_gelar'], 0,12), 
		//	'wk'=>position_text_align( $karyawan['waktu_kerja'] , 'center'), 
		//	'kel'=>position_text_align( $karyawan['kelompok'] , 'center'), 
			 
			 
		);
		$row2  = array(); 
		foreach($tanggals as $tgl){ 
			$dt = get_kelompok_jadwal_by_upload(  $ey['karyawan_id']); 
			if(! isset($dt)){
				$ds =get_shift_normal($tgl);
				$row2[date('d',strtotime($tgl) )] =  position_text_align( $ds , 'right');
			}else{
				$row2[date('d',strtotime($tgl) )] =  position_text_align( $dt['h'.date('d',strtotime($tgl) )] , 'right');
			}
		}
		$row3   = array(' '=> position_text_align( $detail_button  , 'right'));
		$row[] = array_merge( $row1 , $row2 , $row3);
	}
	
	$datas = table_rows($row); 
	$paging = $kgPagerOBJ ->showPaging();
	$view =  form_header( "detail pegawai" , "cp"  );
	$view .= form_field_display( '<span class="label_form">'.  date('d-m-Y' ,strtotime($periode['dari'])) .' &nbsp; s/d &nbsp; '.date('d-m-Y' ,strtotime($periode['sampai'])).'</span>'   , "<b>Periode Absen</b>"    ); 
	$view .= form_footer( );	
	$search = '<form method="GET"><input type="hidden" name="com" value="'.$_GET['com'].'" /><span style="font:10px verdana"><b>Pencarian cepat</b></span><br/><input type="text"  style="width:200px;" name="key" value="'.(isset($_GET['key']) ? $_GET['key']: "").'" /><input class="simple_search" type="submit" value=" CARI " /> </form>';
	$menuontop = array(  
		//	'Excel' => array('onclick'=>'javascript:location.href=\'index.php?com='.$_GET['com'].'&task=excel_data\''),
		//	'Upl Jadwal' =>array('onclick'=>'javascript: jQuery.facebox({ ajax: \'index.php?com='.$_GET['com'].'&task=upload_schedule\' });')
		);
	$printmenu = additional_menu_on_list( $menuontop ,$search);
	return  $view. $printmenu .table_builder($header , $datas ,  15 ,false ,$paging );
}

function get_kelompok_jadwal_by_upload(   $karyawan_id ){
	$query = "SELECT * FROM a_temp_jadwal_karyawan WHERE karyawan_id = {$karyawan_id}";
	$result = my_query($query);
	$row = my_fetch_array($result);
	return $row;
}

function get_shift_normal($tanggal){
	if(get_hari_libur($tanggal)){	
		$datas = 'X';
	}else{ 
		$datas = 'N'; 	
	}
	return $datas;	
}

function get_hari_id_by_date($date){
	$hari_id = date("N",strtotime($date) ); //Monday to Sunday
	//konversi ke Sunday to Saturday
	if($hari_id==7){
		$hari_id=1;
	}else{
		$hari_id++;
	}		
	return $hari_id;
}


function get_hari_libur($date){
	if(is_sabtu_minggu($date)){
		return true;
	}
	$query="SELECT hari_libur_id FROM a_hari_libur
		WHERE tanggal = '{$date}'";
	if (my_num_rows( my_query($query)) >0){	
		return true;
	}	
	return false;
}



function detail_realisasi_karyawan($karyawan_id){
 
	my_set_code_js('
		function submitChange(tanggal ,field ,value){
			location.href=\'index.php?com='.$_GET['com'].'&field=\'+field+\'&value=\'+value+\'&tanggal=\'+tanggal+\'&task=update&karyawan_id='.$karyawan_id.'\'  ;
		}
	');

	$karyawan = loaddata_karyawan($karyawan_id , false);
	$periode_id =get_periode_absen();
	$periode = my_get_data_by_id('pr_periode','periode_id',$periode_id);
	get_set_jadwal_karyawan( $karyawan_id ,$periode );
	$bulan = (int) date('m' , strtotime($periode['tanggal']));
	$tahun = $periode['tahun'] ;
	//$kelompok_shift_id=$karyawan['kelompok_shift_id'] ;
	if($bulan =='1' ){
		$startdate = ($tahun -1).'-12-11';
		$middledata = ($tahun -1).'-12-25';
		$middledata2 = ($tahun -1).'-12-26';
		$enddate = $tahun.'-'.sprintf( '%02d',$bulan) .'-10'; //hasilnya 10
	}else{
		$startdate = $tahun.'-'.sprintf( '%02d',$bulan-1) .'-11';
		$middledata =  $tahun .'-'.sprintf( '%02d',$bulan-1) .'-25';
		$middledata2 =  $tahun .'-'.sprintf( '%02d',$bulan-1) .'-26';
		$enddate =$tahun.'-'.sprintf( '%02d',$bulan) .'-10'; //hasilnya 10
	}
	
	$tanggals = list_kalender($startdate , $middledata  );
	$tanggals = array_unique($tanggals) ;
	
	$header = array(
		'DT'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'),  
		'SCH'=>array('style'=>'text-align:left;border-bottom:2px solid;width:8%'),   
		'LEM'=>array('style'=>'text-align:left;border-bottom:2px solid;width:8%'),   
		'SCH TIME'=>array('style'=>'text-align:left;border-bottom:2px solid;width:27%'),   
		'RLS TIME'=>array('style'=>'text-align:left;border-bottom:2px solid;width:27%'),   
	//	'PC'=>array('style'=>'text-align:left;border-bottom:2px solid;width:14%'),   
	//	'R Time'=>array('style'=>'text-align:left;border-bottom:2px solid;width:23%'),   
		'M'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),   
		'P'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),   
	);
	$row= array();
	$waktu_kerja_id= (int) isset( $karyawan['waktu_kerja_id'] )? $karyawan['waktu_kerja_id'] : 0 ;  
	foreach($tanggals as $tanggal){
		$datas=get_realisasi_karyawan($tanggal , 
			$karyawan_id ) ;	
 
		if(get_hari_libur($tanggal)){	
			$row[] = array(
				'tanggal'	=>position_text_align('<font color="red">'.date('d',strtotime($tanggal)).'</font>','center'),
				's_jadwal'	=>$datas['s_jadwal_kerja'], 
				'ad'	=>$datas['li'], 
				's_time' 	=>$datas['s_time'], 
				'r_status' 	=>$datas['r_realisasi'],
		//		'r_pg' 		=>$datas['r_pg'], 
		//		'r_time'	=>$datas['r_time'], 
				'chk1'		=>position_text_align($datas['chk1'],'center'),
				'chk2'		=>position_text_align($datas['chk2'],'center'),
			);
		}else{	
			$row[] = array(
				'tanggal'	=>position_text_align(date('d',strtotime($tanggal)),'center'),
				's_jadwal'	=>$datas['s_jadwal_kerja'], 
				'ad'	=>$datas['li'], 
				's_time' 	=>$datas['s_time'], 
				'r_status' 	=>$datas['r_realisasi'],
		//		'r_pg' 		=>$datas['r_pg'], 
		//		'r_time'	=>$datas['r_time'], 
				'chk1'		=>position_text_align($datas['chk1'],'center'), 
				'chk2'		=>position_text_align($datas['chk2'],'center'),
			);
		}	
	}
	$datas = table_rows($row);
	$table1 = table_builder($header , $datas ,  2 , false);
	
	$tanggals = list_kalender($middledata2 ,$enddate  );
	$tanggals = array_unique($tanggals) ;
	$row= array(); 
	foreach($tanggals as $tanggal){
		$datas=get_realisasi_karyawan($tanggal , 
			$karyawan_id ) ;	
		if(get_hari_libur($tanggal)){	
			$row[] = array(
				'tanggal'	=>position_text_align('<font color="red">'.date('d',strtotime($tanggal)).'</font>','center'),
				's_jadwal'	=>$datas['s_jadwal_kerja'],
				'ad'		=>$datas['li'],  
				's_time' 	=>$datas['s_time'], 
				'r_status' 	=>$datas['r_realisasi'],
		//		'r_pg' 		=>$datas['r_pg'], 
		//		'r_time'	=>$datas['r_time'], 
				'chk1'		=>position_text_align($datas['chk1'], 'center'),
				'chk2'		=>position_text_align($datas['chk2'],'center'),
			);
		}else{	
			$row[] = array(
				'tanggal'	=>position_text_align(date('d',strtotime($tanggal)),'center'),
				's_jadwal'	=>$datas['s_jadwal_kerja'], 
				'ad'	=>$datas['li'], 
				's_time' 	=>$datas['s_time'], 
				'r_status' 	=>$datas['r_realisasi'],
		//		'r_pg' 		=>$datas['r_pg'], 
		//		'r_time'	=>$datas['r_time'], 
				'chk1'		=>position_text_align($datas['chk1'],'center'), 
				'chk2'		=>position_text_align($datas['chk2'],'center'),
			);
		}	
	}
	$datas = table_rows($row);
	$table2 = table_builder($header , $datas ,  2 , false);
 
	$newline = array(
		'Periode'=>date('d-m-Y',strtotime($periode['dari'])). " s/d ". 
		date('d-m-Y',strtotime($periode['sampai']))
	);
	$view = karyawan_header_detail($karyawan  ,  true , $newline );
	 
	$split ='<div style="height:470px;"><form method="post" name="multiDataBox">
	<div style="width:49%;float:left">'.$table1.'</div>';
	$split .='<div style="width:49%;float:right">'.$table2.'</div></div>';
	$view .= '<div style="width:98%;text-align:right;">
	<!--input type="button" value="Lihat Log" class="main_button" onclick="javascript:location.href=\'index.php?com=a_control&task=absenperkaryawan&karyawan_id='.$karyawan_id.'\'"/ -->
	<!-- input type="button" value="Cetak" class="main_button" onclick="javascript:window.open(\'index.php?com=report&task=rpt_wk_realisasi&karyawan_id='.$karyawan_id.'&'.rand(0,1111).'\',\'mywin\',\'left=20,top=20,width=800,height=600,toolbar=0,resizable=0\');"/ -->
	<input type="button" value="Back" class="main_button" onclick="javascript:location.href=\'index.php?com=a_control\'"/>
	</form></div>'   ; 	 	
	return $view . $split;
}

function  get_realisasi_karyawan($tanggal , $karyawan_id ) {
	list($yyyy,$mm,$dd) = explode("-" , $tanggal  );
	$kode_jadwal = get_data_schedule($dd,$karyawan_id);
	$jam = get_data_time_schedule_by_code($kode_jadwal,$tanggal);
	$reali = get_data_realisasi_time($karyawan_id , $tanggal);
	
	if($reali['correct_in'] == 'N')
		$form_in = '<input type="checkbox" name="check_in"/>';
	else
		$form_in = '<input type="checkbox" name="check_in" checked="checked"/>';
		
	if($reali['correct_in'] == 'N')
		$form_out = '<input type="checkbox" name="check_in"/>';
	else
		$form_out = '<input type="checkbox" name="check_in" checked="checked"/>';
	
	$lembur = get_lembur_karyawan($tanggal  , $karyawan_id); 
	$datas = array(
			's_jadwal_kerja'	=> $kode_jadwal, 
			'li'	=> $lembur, 
			's_time' 	=> date('H:i',strtotime($jam['jam_masuk'])).' <b>s/d</b> '. date('H:i',strtotime($jam['jam_pulang'])), 
			'r_realisasi' 	=> date('H:i',strtotime($reali['jam_in'])).' <b>s/d</b> '. date('H:i',strtotime($reali['jam_out'])),  
			'chk1'		=> $form_in, 
			'chk2'		=> $form_out,
		);
	return $datas;
}

function get_lembur_karyawan($date , $karyawan_id){
	$query = "SELECT jumlah_jam FROM a_spl WHERE karyawan_id = {$karyawan_id} AND tanggal='{$date}' ";
 
	$result = my_query($query);
	$row = my_fetch_array($result);  
	return (float)$row['jumlah_jam'];
}

function get_data_schedule($dd,$karyawan_id){
	$d = (int) $dd;
	$query = "SELECT status_absen FROM temp_tmn_hari_new 
	WHERE karyawan_id = {$karyawan_id} AND tgl_index ='{$d}' ";
	 
	$result = my_query($query);
	$row = my_fetch_array($result);  
	return $row['status_absen'];
}

function get_data_realisasi_time($karyawan_id , $date){
	$query = "SELECT jam_in,	jam_out,
		correct_in, correct_out FROM temp_proses_finger WHERE karyawan_id ={$karyawan_id} AND proses_date ='{$date}'";
	
	$result = my_query($query);
	$row = my_fetch_array($result); 
	return $row;	
}

function get_data_time_schedule_by_code($code,$date){
	 
	$hari_id = 1 + (int) date('w' , strtotime($date) );
	$query = "SELECT jam_masuk , jam_pulang
	FROM  `a_jadwal_kerja` 
	WHERE  kelompok = '{$code}' 
	AND hari_id = '{$hari_id}'
	AND ( `waktu_kerja_id` =4 OR  `waktu_kerja_id` = 1 )
	";
	$result = my_query($query);
	if(my_num_rows($result) > 0){
		$row = my_fetch_array($result);
		return $row;
	}else{
		$datas = array(
			'jam_masuk'=>'00:00:00',
			'jam_pulang'=>'00:00:00',
		);
		return $datas;
	} 
} 


function get_realisasi_schedule($tanggal , $karyawan_id, $waktu_kerja_id,  $kelompok, $kode ,$kelompok_shift_id, $kelompok_shifts){
	$r=array();
	list($yyyy,$mm,$dd) = explode("-" , $tanggal);
	$ch = "SELECT * FROM a_realisasi 
		WHERE karyawan_id = {$karyawan_id} 
			AND field_tgl='tgl_{$dd}' 
			ORDER BY realisasi_id 
			DESC LIMIT 1";
	$chr = my_query($ch);
	$row = my_fetch_array($chr);
	
	$sc_jadwal_kerja_kelompok_id=(int) $row['sc_jadwal_kerja_kelompok_id'];
	$sc_status_absen_id=(int) $row['sc_status_absen_id'];
	$sc_kelompok='0';
	if($sc_jadwal_kerja_kelompok_id==0){
		if($kode=="X"){
			$sc_jadwal_kerja_kelompok_id=21;		
		}else{	
			if($waktu_kerja_id>3){ //kelompoknya A,B.. harus dikonversi ke 1,2,3
				$kelompok=get_kalendar_shift_by_tanggal($tanggal, $kelompok_shift_id );		
			}		
			$query = "SELECT * FROM a_jadwal_kerja_kelompok 
				WHERE waktu_kerja_id = {$waktu_kerja_id}
				AND kelompok='{$kelompok}' ";
			$res = my_query($query);
			$reals = my_fetch_array($res);
			$sc_jadwal_kerja_kelompok_id=(int) $reals['jadwal_kerja_kelompok_id'];		
		}
	}
	
	$kelompok_shift = array(
		'name'=>'kelompok_shift_id',
		'id'=>'kelompok_shift_id',
		'onchange'=>'submitChange(\''.'tgl_'.$dd.'\' , \'sc_jadwal_kerja_kelompok_id\' ,this.value  )',
		'value'=>$sc_jadwal_kerja_kelompok_id  
		);
	$r['form_wk'] = form_dropdown($kelompok_shift,$kelompok_shifts) ;
	
	$r['time']=''; //init
	if($sc_jadwal_kerja_kelompok_id>0 AND $sc_jadwal_kerja_kelompok_id<21){
		$jadwal_kerja_kelompoks = my_get_data_by_id('a_jadwal_kerja_kelompok','jadwal_kerja_kelompok_id',$sc_jadwal_kerja_kelompok_id);
		$hari_id=get_hari_id_by_date($tanggal);
		$query = "SELECT jam_masuk,jam_pulang FROM a_jadwal_kerja 
				WHERE waktu_kerja_id =".$jadwal_kerja_kelompoks['waktu_kerja_id']."  
				AND kelompok='".$jadwal_kerja_kelompoks['kelompok']."' 
				AND hari_id={$hari_id}";
		$jams = my_fetch_array(my_query($query));	
		$r['time']=date('H:i',strtotime($jams['jam_masuk'])).' - '.date('H:i',strtotime($jams['jam_pulang'])); 
	}
	$statuss=get_status_absen($karyawan_id , $tanggal );
	
	$s='' ;
	foreach($statuss as $status){
		$s.=$status.', ' ;
	}
	$s=substr($s,0,strlen($s)-2) ;	
	$r['status']=$s ;

	return $r;	
}


function get_set_jadwal_karyawan( $karyawan_id ,$periode ){
	$query = "SELECT * FROM a_temp_jadwal_karyawan WHERE karyawan_id = {$karyawan_id}";
	$result = my_query($query);
	if( my_num_rows($result) > 0){
		return false;
	}
	 
	$periodes = list_kalender($periode['dari'] , $periode['sampai']  ) ; 
	$datas1 = array(
		'karyawan_id'=>my_type_data_int($karyawan_id)
	);
	$datas2 = array();
	foreach($periodes as $tgl){
		list($yyy,$mm,$dd) = explode("-" , $tgl);
		$datas2['h'.$dd] = my_type_data_str( get_shift_normal($tgl ) ) ; 
	}
	$datas = array_merge($datas1, $datas2 );
	return my_insert_record('a_temp_jadwal_karyawan' , $datas);
}